//
//  LGSchedulSettingController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/2/28.
//  设置排班

#import "LGSchedulSettingController.h"

#import "LGAddSchedulController.h"
//设置排班
#import "LGSchedulSettingController.h"
//团队咨询设置
#import "LGTeamConsultSettingVC.h"

#import "LGSchedulHeaderView.h"
#import "LGSchedulFooterView.h"
#import "LGSchedulHomePriceCell.h"
#import "LGSchedulHomeTimeCell.h"
#import "LGSchedulHomeConsultCell.h"
#import "LGPickerView.h"
@interface LGSchedulSettingController ()<UITableViewDataSource,UITableViewDelegate,LGSchedulHeaderDelegate>
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , strong)NSArray *offlineArray;
/**   */
@property (nonatomic , strong)NSArray *videoArray;
/**   */
@property (nonatomic , strong)NSArray *expertArray;
/**   */
@property (nonatomic , strong)NSDictionary *teamDic;
/**  视频咨询价格选项 */
@property (nonatomic , strong)NSArray *videoPrice;
/**  专家咨询价格选项 */
@property (nonatomic , strong)NSArray *expertPrice;
/**  运动评估价格选项 */
@property (nonatomic , strong)NSArray *sportsPrice;
/**   */
@property (nonatomic , copy)NSString *priceType;

@end

@implementation LGSchedulSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"排班设置";
    self.priceType = @"";
    [self.view addSubview:self.tableView];
    [self emptyViewWithSuperView:self.view frame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight)];
    [self requestData];
    [self requestPriceData];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/visit.Visit/serviceList" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        if(code == 200){
            self.dataArray = returnData[@"visit"];
            self.teamDic = returnData[@"team"];
        }else if (self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:message offset:0];
        }
        [self.tableView reloadData];
    } error:^(NSError *error) {
        self.isfirst = NO;
        if (self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:LGErrorMessage offset:0];
        }
    }];
}

-(void)requestPriceData{
    [RequestUtil withPOST:@"/common/Dict/consultPriceOption" parameters:@{} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            self.videoPrice = [NSArray arrayWithArray:returnData[@"consult_video_price"][@"options"]];
            self.expertPrice = [NSArray arrayWithArray:returnData[@"consult_expert_price"][@"options"]];
            self.sportsPrice = [NSArray arrayWithArray:returnData[@"consult_sport_price"][@"options"]];
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)savePriceData:(NSString *)price{
    NSDictionary *parameters = @{@"clinic_type":self.priceType,@"price":price};
    [RequestUtil withPOST:@"/doctor/visit.Visit/saveService" parameters:parameters withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if(code == 200){
            [self requestData];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:LGErrorMessage offset:0];
    }];
}

-(void)openOrCloseServiceType:(NSString *)type isClosed:(NSString *)isClosed{
    [RequestUtil withPOST:@"/doctor/visit.Visit/openOrCloseService" parameters:@{@"clinic_type":type , @"is_closed":isClosed} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            [self requestData];
        }else{
            [TooltipView showMessage:message offset:0];
        }
    } error:^(NSError *error) {
        [TooltipView showMessage:LGErrorMessage offset:0];
    }];
}

//添加排班
-(void)addSchedulAction:(UIButton *)sender{
    NSInteger index = sender.tag - 1000;
    NSDictionary *tempDic = self.dataArray[index];
    LGAddSchedulController *controller = [[LGAddSchedulController alloc]init];
    controller.type = LGNSString(tempDic[@"clinic_type"]);
    [self.navigationController pushViewController:controller animated:YES];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSDictionary *tempDic = self.dataArray[section];
    NSString *type = LGNSString(tempDic[@"clinic_type"]);
    NSArray *tempArray = tempDic[@"schedules"];
    if ([LGNSString(tempDic[@"is_closed"]) isEqualToString:@"1"]){
        //关闭状态
        return 0;
        
    }else if ([type isEqualToString:@"1"]||[type isEqualToString:@"5"]){
        //线下复查、运动评估
        return tempArray.count;
        
    }else{
        //视频门诊 、专家咨询
        return tempArray.count+1;
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.section];
    NSString *type = LGNSString(tempDic[@"clinic_type"]);
    NSArray *tempArray = tempDic[@"schedules"];
    if(!([type isEqualToString:@"1"]||[type isEqualToString:@"5"]) && indexPath.row == 0){
        //价格
        LGSchedulHomePriceCell *priceCell = [tableView dequeueReusableCellWithIdentifier:@"priceCell" forIndexPath:indexPath];
        priceCell.selectionStyle = UITableViewCellSelectionStyleNone;
        priceCell.price = LGNSString(tempDic[@"price"]);
        return priceCell;
    }else{
        LGSchedulHomeTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:@"timeCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        NSInteger index = ([type isEqualToString:@"1"]) ? indexPath.row : indexPath.row - 1;
        cell.showTitle = (index == 0)?YES:NO;
        cell.dataDic = tempArray[index];
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //设置门诊排班
    NSDictionary *tempDic = self.dataArray[indexPath.section];
    NSString *type = LGNSString(tempDic[@"clinic_type"]);
    if(![type isEqualToString:@"1"] && indexPath.row == 0){
        //设置价格
        NSDictionary *arrayDic = @{@"2":self.videoPrice,@"3":self.expertPrice,@"5":self.sportsPrice};
        NSArray *pickArray = arrayDic[type];
        NSMutableArray *tempArray = [NSMutableArray array];
        for (NSInteger i=0; i<pickArray.count; i++) {
            [tempArray addObject:LGNSString(pickArray[i][@"value"])];
        }
        if (tempArray.count == 0) {
            return;//安全判断
        }
        self.priceType = type;
        LGPickerView *pickView = [[LGPickerView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
        pickView.pickArry = @[tempArray];
        pickView.selectStr = LGNSString(pickArray[0][@"value"]);
        pickView.units = @[@"元"];
        [[UIApplication sharedApplication].keyWindow addSubview:pickView];
        __weak typeof(self) weakSelf = self;
        pickView.selectPickViewResult = ^(NSArray *resultArray) {
            [weakSelf savePriceData:LGNSString(resultArray[0])];
        };
       
    }else{
        LGAddSchedulController *controller = [[LGAddSchedulController alloc]init];
        controller.type = LGNSString(tempDic[@"clinic_type"]);
        NSInteger index = indexPath.row;
        if(!([type isEqualToString:@"1"])){
            index = indexPath.row - 1;
        }
        controller.scheduleIds = tempDic[@"schedules"][index][@"schedule_ids"];
        [self.navigationController pushViewController:controller animated:YES];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(85);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    LGSchedulHeaderView *headerView = [[LGSchedulHeaderView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(85))];
    headerView.dataDic = self.dataArray[section];
    headerView.delegate = self;
    return headerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    NSDictionary *tempDic = self.dataArray[section];
    NSString *type = LGNSString(tempDic[@"clinic_type"]);
    if ([type isEqualToString:@"3"] || [LGNSString(tempDic[@"is_closed"]) isEqualToString:@"1"]){
        //专家咨询 || 咨询关闭状态
        return viewPix(15);
        
    }else{
        //线下门诊、视频门诊
        return viewPix(60);
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    NSDictionary *tempDic = self.dataArray[section];
    NSString *type = LGNSString(tempDic[@"clinic_type"]);
    if ([type isEqualToString:@"3"] || [LGNSString(tempDic[@"is_closed"]) isEqualToString:@"1"]){
        //专家咨询
        LGCornerFooterView *footerView = [[LGCornerFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(10))];
        footerView.bottomMargin = 10;
        return footerView;
        
    }else{
        //线下门诊、视频门诊
        LGSchedulFooterView *footerView = [[LGSchedulFooterView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(60))];
        footerView.addButton.tag = 1000+section;
        [footerView.addButton addTarget:self action:@selector(addSchedulAction:) forControlEvents:UIControlEventTouchUpInside];
        return footerView;
    }
}


-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGSchedulHomeTimeCell class] forCellReuseIdentifier:@"timeCell"];
        [_tableView registerClass:[LGSchedulHomePriceCell class] forCellReuseIdentifier:@"priceCell"];
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(30))];
    }
    return _tableView;
}

@end
